package com.red.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.red.context.BaseContext;
import com.red.entity.Rankings;
import com.red.entity.Users;
import com.red.mapper.RankingsMapper;
import com.red.mapper.UserMapper;
import com.red.service.RankingsService;
import com.red.vo.RankingsVO;
import com.red.vo.RankingsViewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class RankingsServiceImpl extends ServiceImpl<RankingsMapper, Rankings> implements RankingsService {

    @Autowired
    private RankingsMapper rankingsMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 答题结果
     * @param score
     */
    public void questionResult(int score) {
        Long userId = BaseContext.getCurrentId();

        QueryWrapper<Rankings> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        Rankings rankings = rankingsMapper.selectOne(qw);
        //查新数据库 如果查不到数据 说明没打过题 新建
        if (rankings == null){
            rankings = new Rankings();
            rankings.setScore(score);
            rankings.setUserId(userId);

            rankings.setUpdatedAt(LocalDateTime.now());
            rankingsMapper.insert(rankings);
        }else {
            //如果查到数据 更新数据
            rankings.setScore(rankings.getScore()+score);
            rankings.setUpdatedAt(LocalDateTime.now());
            rankingsMapper.updateById(rankings);
        }

    }

    /**
     * 查询排行榜
     *
     * @return
     */
    public RankingsViewVO getAll() {
        QueryWrapper<Rankings> qw = new QueryWrapper<>();
                qw.orderByDesc(true,"score");
        List<Rankings> rankings = rankingsMapper.selectList(qw);

        //把查询的赋值给vo
        List<RankingsVO> voList = new ArrayList<>();

        for (Rankings ranking : rankings) {
            //查询用户信息赋值给VO
            RankingsVO rankingsVO = new RankingsVO();
            Users users = userMapper.selectById(ranking.getUserId());
            BeanUtils.copyProperties(ranking,rankingsVO);

            rankingsVO.setAvatar_url(users.getAvatarUrl());
            rankingsVO.setNickname(users.getNickname());
            voList.add(rankingsVO);
        }

        //如果小于三条数据
        if (rankings.size()<3){
            RankingsViewVO rankingsViewVO = new RankingsViewVO();
            rankingsViewVO.setTop3(voList);
            return rankingsViewVO;
        }


        ArrayList<RankingsVO> arrayList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            arrayList.add(voList.get(0));
            voList.remove(0);
        }
        RankingsViewVO rankingsViewVO = new RankingsViewVO();
        rankingsViewVO.setTop3(arrayList);

        rankingsViewVO.setOtherRankings(voList);

        return rankingsViewVO;
    }
}
